package com.bilibili.lzx.service;

import com.bilibili.lzx.dao.UserFollowingDao;
import com.bilibili.lzx.dao.domain.FollowingGroup;
import com.bilibili.lzx.dao.domain.User;
import com.bilibili.lzx.dao.domain.UserFollowing;
import com.bilibili.lzx.dao.domain.UserInfo;
import com.bilibili.lzx.dao.domain.constant.UserConstant;
import com.bilibili.lzx.dao.domain.exception.ConditionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lzx
 * @date 2024/8/20 20:58
 */
@Service
public class UserFollowingService {

    @Resource
    private UserFollowingDao userFollowingDao;

    @Resource
    private FollowingGroupService followingGroupService;

    @Resource
    private UserService userService;

    @Transactional(rollbackFor = Exception.class)
    public void addUserFollowing(@RequestBody UserFollowing userFollowing){
        System.out.println(userFollowing);
        if(userFollowing.getUserId().equals(userFollowing.getFollowingId())){
            throw new ConditionException("自己不能关注自己");
        }
        Long groupId = userFollowing.getGroupId();
        if(groupId==null){
            FollowingGroup followingGroup = followingGroupService.getByType(UserConstant.USER_FOLLOWING_GROUP_TYPE_DEFAULT);
            userFollowing.setGroupId(followingGroup.getId());
        }
        FollowingGroup followingGroup = followingGroupService.getById(userFollowing.getGroupId());
        if(followingGroup==null){
            throw new ConditionException("关注分组不存在");
        }
        User user = userService.getById(userFollowing.getFollowingId());
        if(user==null){
            throw new ConditionException("关注的用户不存在");
        }
        if(userFollowingDao.getUserFollowingByUserIdAndFollowingId(userFollowing.getFollowingId(),userFollowing.getUserId())!=null){
            userFollowing.setEachFollowing(true);
            userFollowingDao.setEachFollowing(userFollowing.getFollowingId(),userFollowing.getUserId());
        }else {
            userFollowing.setEachFollowing(false);
        }
        userFollowingDao.deleteUserFollowing(userFollowing.getUserId(),userFollowing.getFollowingId());
        userFollowing.setCreateTime(new Date());
        userFollowingDao.addUserFollowing(userFollowing);
    }

    public List<FollowingGroup> getUserFollowings(Long userId) {
        List<UserFollowing> followingList = userFollowingDao.getUserFollowings(userId);
        Set<Long> followingIdSet = followingList.stream().map(UserFollowing::getFollowingId).collect(Collectors.toSet());
        List<UserInfo> userInfoList = new ArrayList<>();
        if(followingIdSet.size()>0) {
           userInfoList = userService.getUserInfoByUserIds(followingIdSet);
        }
        for (UserFollowing userFollowing:followingList){
            for (UserInfo userInfo:userInfoList){
                if(userFollowing.getFollowingId().equals(userInfo.getUserId())){
                    userFollowing.setUserInfo(userInfo);
                }
            }
        }
        List<FollowingGroup> result = followingGroupService.getByUserId(userId);

        for(FollowingGroup followingGroup:result){
            List<UserFollowing> userFollowings = new ArrayList<>();
            for (UserFollowing userFollowing:followingList){
                  if (followingGroup.getId().equals(userFollowing.getGroupId())){
                      userFollowings.add(userFollowing);
                  }
            }
            followingGroup.setUserFollowingList(userFollowings);
        }
        FollowingGroup allFollowingGroup = new FollowingGroup();
        allFollowingGroup.setName(UserConstant.FOLLOWING_GROUP_ALL_NAME);
        allFollowingGroup.setUserFollowingList(followingList);
        result.add(allFollowingGroup);
        return result;
    }

    public List<UserFollowing> getUserFans(Long userId){
        List<UserFollowing> userFans = userFollowingDao.getUserFans(userId);
        Set<Long> followingIdSet = userFans.stream().map(UserFollowing::getUserId).collect(Collectors.toSet());
        List<UserInfo> userInfoList = new ArrayList<>();
        if (followingIdSet.size()>0){
            userInfoList = userService.getUserInfoByUserIds(followingIdSet);
        }
        for (UserFollowing userFan : userFans) {
            for (UserInfo userInfo : userInfoList) {
                if(userFan.getUserId().equals(userInfo.getUserId())){
                    userFan.setUserInfo(userInfo);
                }
            }
        }
        return userFans;
    }

}
